//vars
var WIDTH = 730;
var HEIGHT = 600;


var DEFAULT_FPS = 1;
var DEFAULT_CANDIES = 100;


//model
function GModel() {
	var view = null;
	this.setView = function(v) { view = v; };
	this.getView = function() { return view; };

	this.init = function() {
        cndCount = DEFAULT_CANDIES;
	};


	var paused = false;
	this.paused = function() { return paused; };

	this.tooglePause = function() {
		paused = !paused;
	};

		// build next/new frame
	this.next = function() {
		if (paused) {
            cndUp();
            view.update();
			return;
		}

        cndDown();


        view.update();
 	};

 	this.clear = function() {
		context.clearRect(0, 0, WIDTH, HEIGHT);
	};

	var cndUp = function() {
        console.log(cndCount);
		cndCount++;
        view.drawCount(cndCount);




	};
	var cndDown = function() {
        console.log(cndCount);
		cndCount--;
	};

}




//view
function GView(resources) {
    var context = resources.CANVAS.getContext("2d");
	context.font = "bold 18px/2.8 arial";
    var time = new Date().getTime();
	var FPS_REFRESH_INTERVAL = 10;
	var frameCounter = 0;

	var model = null;
	this.setModel = function(m) { model = m; };
	this.getModel = function() { return model; };



    this.play = function() {
        resources.CANVAS.style.display="block";

    };

    this.drawCount = function(cndCount) {
        var digits = String(cndCount).split("");
        obj = {
            a : digits[0],
            b : digits[1],
            c : digits[2]
        };


    };
    this.update = function() {
        if (model.paused()) {
            message("PAUSED");
            console.log('2');
            return;
        }

		// draw background
		context.clearRect(0, 0, WIDTH, HEIGHT);

        console.log('1');
		// draw time

        var currentTime = new Date().getTime();
        var winev = (currentTime - time)/1000*DEFAULT_FPS;
//		var time = (frameCounter / totalTime) * 1000.0;
		context.fillText(winev.toFixed(0) + " сек", 4, HEIGHT - 6);
		context.fillStyle = "#ffffff";




    };


	var message = function(text) {

		context.save();
		context.textAlign = "center";
		context.font = "bold 26px/3.8 arial";
		context.fillStyle= "#777777";
		context.fillText(text, WIDTH / 2, HEIGHT / 2);
		context.restore();
	};
}







function Game(model, view, ctrl) {
    var self = this;
    var interrupter = null;
    view.setModel(model);
    model.setView(view);


    this.play = function() {
        view.play();
        model.init();
        interrupter = setInterval(model.next, 1000/DEFAULT_FPS)
    };
    this.pause = function() {
        model.tooglePause(); 
    };
    this.clear = function() {
        model.clear();
    };
}
